import request from '@/utils/request'
import qs from "qs";

export function getFiles (module, form, part) {
  const params = { module: module, form: form, part: part }
  return request.get('file-server/file/part/getFiles?' + qs.stringify(params, { indices: false }))
}

export function getFileByModuleAndForm (module, form) {
  const params = { module: module, form: form }
  return request.get('file-server/file/getFiles?' + qs.stringify(params, { indices: false }))
}

export function deleteFile (id) {
  return request.post('file-server/file/deleteFile?id=' + id)
}

export function uploadAttenceFile (data) {
  return request.post('buss/attence/upload', data, {
    headers: {
      'Content-Type':
        'multipart/form-data; boundary=----WebKitFormBoundaryIOi8qEBAFV1KN25n'
    }
  })
}

export function uploadFile (data) {
  return request.post('file-server/file/uploadBatch', data, {
    headers: {
      'Content-Type':
        'multipart/form-data; boundary=----WebKitFormBoundaryIOi8qEBAFV1KN25n'
    }
  })
}

export function download (id) {
  return request.get('file-server/file/download?id=' + id, { responseType: 'blob' })
}

export function updateForm (oldId, newId) {
  return request.post('file-server/file/updateFormId', {
    oldFormId: oldId,
    newFormId: newId
  })
}

// 这地方不走代理，用原生, 否则在手机上上传非常慢
const baseUri = process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API : `http://${window.location.host}`
export const uploadUrl = baseUri + '/file-server/file/uploadBatch'
// --------------------分割：以下为列表-----------------------------

export function addTable (data) {
  return request.post('file-server/file/addTable', data)
}

export function deleteTable (data) {
  return request.post('file-server/file/deleteTable', data)
}

export function editTable (data) {
  return request.post('file-server/file/editTable', data)
}

export function getTablePage (data) {
  return request.post('file-server/file/listPage', data)
}
export function packZip (data) {
  return request.post('file-server/file/packZip?ids=' + data.ids, data, { responseType: 'blob' })
}

export function preview (id) {
  return request.post('file-server/file/preview?id=' + id)
}

export function uploadPre (data) {
  return request.post('file-server/file/uploadPre', data, {
    headers: {
      'Content-Type':
        'multipart/form-data; boundary=----WebKitFormBoundaryIOi8qEBAFV1KN25n'
    }
  })
}

export function changeBind (fileId, module, form, part) {
  return request.post(`file-server/file/changeBind?fileId=${fileId}&module=${module}&form=${form}&part=${part}`, {}, {
    headers: {
      'Content-Type':
        'multipart/form-data; boundary=----WebKitFormBoundaryIOi8qEBAFV1KN25n'
    }
  })
}
export default { changeBind, addTable, deleteTable, editTable, uploadAttenceFile, uploadFile, preview, getTablePage, getFiles, updateForm, download, deleteFile, getFileByModuleAndForm }
